﻿using FinMkts.Messaging.Pgm;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;

namespace FinMkts.Messaging.UnitTests
{


    [TestClass()]
    public class ByteBufferCollectionTest
    {
        public TestContext TestContext { get; set; }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void Constructor_SetBytesToZero()
        {
            var target = new ByteBufferCollection();
            Assert.AreEqual(0, target.TotalBytes);
        }

        [TestMethod()]
        public void Add_Increments_TotalBytes()
        {
            var target = new ByteBufferCollection(); 
            var array = new byte[10];
            target.Add(new ArraySegment<byte>(array));
            Assert.AreEqual(1, target.Count);
            Assert.AreEqual(array.Length, target.TotalBytes);
        }

        [TestMethod()]
        public void Set_Changes_TotalBytes()
        {
            var target = new ByteBufferCollection(); 
            var old = new byte[10];
            target.Add(new ArraySegment<byte>(old));
            var array = new byte[20];
            target[0] = new ArraySegment<byte>(array);
            Assert.AreEqual(1, target.Count);
            Assert.AreEqual(array.Length, target.TotalBytes);
        }

        [TestMethod()]
        public void Remove_Decrements_TotalBytes()
        {
            var target = new ByteBufferCollection(); 
            var old = new byte[10];
            target.Add(new ArraySegment<byte>(old));
            target.RemoveAt(0);
            Assert.AreEqual(0, target.Count);
            Assert.AreEqual(0, target.TotalBytes);
        }

        [TestMethod()]
        public void Clear_SetsTotalBytesToZero()
        {
            var target = new ByteBufferCollection(); 
            var old = new byte[10];
            target.Add(new ArraySegment<byte>(old));
            target.Clear();
            Assert.AreEqual(0, target.Count);
            Assert.AreEqual(0, target.TotalBytes);
        }

        [TestMethod, ExpectedException(typeof(ArgumentNullException))]
        public void AddRange_Throws_ForNullItems()
        {
            var target = new ByteBufferCollection(); 
            target.AddRange(null);
        }

        [TestMethod]
        public void AddRange_IncrementsTotalBytes()
        {
            var target = new ByteBufferCollection(); 
            var queue = new[] {new byte[1], new byte[2],};
            target.AddRange(queue);
            Assert.AreEqual(2, target.Count);
            Assert.AreEqual(3, target.TotalBytes);

        }

    }
}
